package com.renwei.yunlong.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.NewDetailsTypesAdapter;
import com.renwei.yunlong.base.BaseLazyFragment;
import com.renwei.yunlong.bean.NewDetailsTypesBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;


/**
 * 对象详情  类型  页面
 * <p>
 * Created by Administrator on 2017/9/7.
 */

public class ObjectTypesFragment extends BaseLazyFragment implements HttpTaskListener {


    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    NewDetailsTypesAdapter adapter;
    @Bind(R.id.tv_key)
    TextView tvKey;
    @Bind(R.id.tv_value)
    TextView tvValue;
    private String outSourceFlag;
    private String assetId;

    public ObjectTypesFragment(String outSourceFlag, String assetId) {
        this.outSourceFlag = outSourceFlag;
        this.assetId = assetId;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_new_details_types;
    }

    @Override
    public void onLazyLoad() {
        initView();
        initData();
    }

    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new NewDetailsTypesAdapter(getContext());
        recyclerView.setAdapter(adapter);
    }

    public void initData() {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {//业主
            if ("0".equals(outSourceFlag)) {
                map.put("currentUserId", ownerBean.getRows().getEmployeeId());
            }
        } else if ("2".equals(companyType)) {//服务商
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
        }
        map.put("assetId", assetId);
        map.put("page", "1");
        map.put("rows", "100000");
        ServiceRequestManager.getManager().getObjectType(getContext(), outSourceFlag,
                JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        NewDetailsTypesBean typesBean = new Gson().fromJson(result, NewDetailsTypesBean.class);
        if (typesBean.getMessage().getCode() == 200) {
            tvValue.setText(typesBean.getRows().getParentTypeName());
            List<NewDetailsTypesBean.RowsBean.PropertyListBean> list = typesBean.getRows().getPropertyList();
            if (list == null) {
                list = new ArrayList<>();
            }
            NewDetailsTypesBean.RowsBean.PropertyListBean bean = new NewDetailsTypesBean.RowsBean.PropertyListBean();
            bean.setNamea("子级类型");
            bean.setValuea(typesBean.getRows().getTypeName());
            list.add(0, bean);
            adapter.setData(list);
        }
    }

    @Override    public void onException(int requestId, String code) {        showTopWrongMsg(getResources().getString(R.string.net_work_error));    }
}
